<?php

namespace App\Admin\Controllers\Merchant;

use App\Admin\Renderable\ShopUserRenderable;
use App\Models\SwtMerchant;
use App\Services\AdminService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Validation\Rule;

class SwtMerchantController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(SwtMerchant::with('machines'), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('shop_user_id','来源') -> display( function($value) {
                return $value ? '商城' : '-';
            });
            $grid->column('machine_num') -> display( function() {
                return count($this->machines).'台';
            });
            $grid->column('virtual_device_num') -> display( function($value) {
                return $value.'台';
            });
            $grid->column('mobile');
            $grid->column('profit_amount') -> display(function ($value) {
                return bcdiv($value, 100 ,2);
            });
            $grid->column('sale_amount') -> display(function ($value) {
                return bcdiv($value, 100 ,2);
            });
            $grid->column('compensation');
            $grid->column('deposit');
            $grid->column('status')->switch();
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('mobile');
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions -> quickEdit();
            });

            $grid->quickSearch('mobile');

            $grid->disableCreateButton(false);
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(SwtMerchant::class, function (Form $form) {
            $form->display('id');

            $shop_user_id = '';

            if($form -> isEditing()) {
                $shop_user_id = $form -> model() -> shop_user_id;
            }

            $form -> selectTable('shop_user_id')
                -> title('关联商城用户')
                -> dialogWidth('70%')
                -> from(ShopUserRenderable::make(['id'=>$shop_user_id]))
                -> rules(function (Form $form) {
                    $id = $form->model()->id;
                    return [
                        Rule::unique('swt_merchant')->whereNotNull('shop_user_id')->ignore($id)->withoutTrashed()
                    ];
                },['unique'=>'商城用户重复'])
                -> options(function($v) {
                    if (!$v) {
                        return [];
                    }
                    $user_info = AdminService::getUserInfo($v[0]);
                    if($user_info['data']) {
                        return [$user_info['data']['id'] => $user_info['data']['nickname']];
                    } else {
                        return [];
                    }
                })
                -> pluck('nickname');

            $form->text('name')
                ->rules(function (Form $form) {
                    $id = $form->model()->id;
                    return [
                        'required',
                        Rule::unique('swt_merchant')->ignore($id)->withoutTrashed()
                    ];
                },['unique'=>'商户姓名重复'])
                -> required();

            $form->text('mobile')
                ->rules(function (Form $form) {
                    $id = $form->model()->id;
                    return [
                        Rule::unique('swt_merchant')->whereNotNull('mobile')->ignore($id)->withoutTrashed()
                    ];
                },['unique'=>'联系电话重复']);

            $form->number('virtual_device_num')->min(0);

            $form->number('compensation')
                ->min(0)
                ->rules(['required'])
                ->required();

            $form->number('deposit')
                ->min(0)
                ->rules(['required'])
                ->required();
        });
    }

}
